<template>
  <zeda-dialog
    v-model="visible"
    :title="(id <= 0 ? '新建' : '编辑') + ''"
    width="1000px"
    :before-close="handleClose"
  >
    <formCom
      ref="formCom"
      @formDatagive="formDatagive"
      :disabledFlage="false"
    ></formCom>
    <form-button
      slot="footer"
      :loading="loading"
      @cancel="handleClose"
      @submit="submit"
    >
    </form-button>
  </zeda-dialog>
</template>

<script>
import { add, edit, detail } from "@/api/sensingEquipment/index.js";

import FormButton from "@/components/FormButton";
import formCom from "./form";

export default {
  dicts: [],
  components: {
    FormButton,
    formCom,
  },
  props: {
    id: {
      type: [Number, String],
      default: "",
    },
    visible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      loading: false, // 按钮加载中判断
      formData: {},
    };
  },
  watch: {
    visible(value) {
      if (value) {
        if (String(this.id).length > 0) {
          this.getInfo();
        }
      }
    },
  },
  methods: {
    getInfo() {
      detail(this.id)
        .then((res) => {
          if (res.code === 700) {
            this.$message.warning(res.msg);
            return false;
          }
          let newData = res?.data ?? {};
          console.log(newData, "newData");
          // newData.areaId = res?.data?.douDistrictId;
          this.$nextTick(() => {
            this.$refs?.formCom.setForm(newData);
          });
        })
        .catch(() => {
          // this.$message.error("系统错误,请联系管理员!");
        });
    },
    submit() {
      this.$refs.formCom.getfromData();
      this.$refs.formCom.$children[0].$refs.form.validate((valid) => {
        if (valid) {
          this.$nextTick(() => {
            const data = {
              ...this.formData,
            };
            this.loading = true;
            if (this.id) {
              this.editRequest(data);
            } else {
              this.addRequest(data);
            }
          });
        } else {
          return false;
        }
      });
    },
    formDatagive(data) {
      // console.log(data);
      this.formData = data;
    },
    addRequest(data) {
      add(data)
        .then((res) => {
          this.loading = false;
          if (res.code === 700) {
            this.$message.warning(res.msg);
            return false;
          }
          this.$message.success("操作成功");
          this.handleClose(true);
        })
        .catch(() => {
          this.loading = false;
          // this.$message.error("系统错误,请联系管理员!");
        });
    },
    editRequest(data) {
      edit(data)
        .then((res) => {
          this.loading = false;
          if (res.code === 700) {
            this.$message.warning(res.msg);
            return false;
          }
          this.$message.success("操作成功");
          this.handleClose(true);
        })
        .catch(() => {
          this.loading = false;
          // this.$message.error("系统错误,请联系管理员!");
        });
    },
    handleClose(status) {
      this.$refs.formCom.resetForm();
      this.$emit("update:visible", false);
      this.$emit("close", status === true ? status : false);
    },
  },
};
</script>

<style scoped lang="scss">
::v-deep .el-form {
  .el-input,
  .el-cascader,
  .el-input-number,
  .el-select {
    width: 100% !important;
  }
  .map {
    display: flex;
    .el-input {
      margin-right: 10px;
    }
  }
}
</style>
